package cn.doitedu.datayi.flume;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.util.List;
import java.util.Random;

public class RandomChannelMappingInterceptor  implements Interceptor {
    Random random ;
    @Override
    public void initialize() {
        random = new Random();
    }

    @Override
    public Event intercept(Event event) {

        int ch = random.nextInt(100) % 4;
        event.getHeaders().put("ch",ch+"");

        return event;
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        for (Event event : events) {
            intercept(event);
        }
        return events;
    }

    @Override
    public void close() {

    }

    public static class RandomChannelMappingInterceptorBuilder implements Interceptor.Builder{

        @Override
        public Interceptor build() {
            return new RandomChannelMappingInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }


}
